# -*- coding: utf-8 -*-
__author__ = 'yangq'

from appcommon.appinstance import mysql_app_pool
from tornado import gen


@gen.coroutine
def save_ad_type(ad_id, ad_type):
    sql = (
        "update app_ad_info set {} where id = {} "
    )

    param0 = "circle=1," if '0' in ad_type else "circle=0,"
    param1 = "one=1," if '1' in ad_type else "one=0,"
    param2 = "two=1," if '2' in ad_type else "two=0,"
    param3 = "three=1," if '3' in ad_type else "three=0,"
    param4 = "four=1, " if '4' in ad_type else "four=0, "
    param5 = "vip=1, " if '5' in ad_type else "vip=0, "
    param6 = "`index`=1, " if '6' in ad_type else "`index`=0, "
    param7 = "couch_index=1, " if '7' in ad_type else "couch_index=0, "
    param8 = "student_index=1, " if '8' in ad_type else "student_index=0, "
    param9 = "couch_circle=1, " if '9' in ad_type else "couch_circle=0, "
    param10 = "pub_consult=1" if '10' in ad_type else "pub_consult=0"
    param = param0 + param1 + param2 + param3 + param4 + param5 + param6 + param7 + param8 + param9 + param10
    res = yield mysql_app_pool.update(sql.format(param, ad_id))
    raise gen.Return(res)


@gen.coroutine
def add_ad_2_mysql(title_url, title, content, current_time, type, url):
    sql = (
        "insert into app_ad_info (title_url, title, content, pubtime, type, url) "
        u"values ('{}','{}', %s, {}, '{}', '{}')"
    )
    yield mysql_app_pool.update(sql.format(
        title_url, title, current_time, type, url
    ), content)
    res = True

    raise gen.Return(res)


@gen.coroutine
def get_ad_detail(ad_id):
    sql = "select id as ad_id, title_url,title, pubtime, content, type, url from app_ad_info where id={}"
    info = yield mysql_app_pool.get_one(sql.format(ad_id), cursor='dict')
    raise gen.Return(info)


@gen.coroutine
def update_ad_2_mysql(title_url, title, content, ad_id, current_time, type, url):
    sql = u"update app_ad_info set title_url = '{}', title = '{}', content=%s, pubtime={}, type='{}', url='{}'  where id = {} "
    try:
        print sql.format(title_url, title, current_time, type, url, ad_id)
        yield mysql_app_pool.update(sql.format(title_url, title, current_time, type, url, ad_id), content)
        res = True
    except Exception, e:
        print e
        res = False
    raise gen.Return(res)


@gen.coroutine
def get_ad_list():
    sql = (
        "select id as ad_id, title_url, title, pubtime, content, circle, one, two, three, four, vip, type,"
        "`index`, couch_index, student_index,couch_circle,pub_consult from app_ad_info order by pubtime desc "
    )
    print sql
    info = yield mysql_app_pool.get_all(sql, cursor='dict')
    raise gen.Return(info)


@gen.coroutine
def del_ad_2_mysql(ad_id):
    sql = "delete from app_ad_info where id = {}"
    info = yield mysql_app_pool.delete(sql.format(ad_id))
    raise gen.Return(info)


@gen.coroutine
def get_share_pic():
    sql = (
        "select * from share_pic limit 1"
    )
    info = yield mysql_app_pool.get_one(sql, cursor='dict')
    raise gen.Return(info)


@gen.coroutine
def update_share_pic(pic_url, pic_type):
    if pic_type == '1':
        param = "share_pic = '{}'".format(pic_url)
    elif pic_type == '2':
        param = "couch_share_pic = '{}'".format(pic_url)
    elif pic_type == '3':
        param = "bottom_pic = '{}'".format(pic_url)
    else:
        param = "couch_bottom_pic = '{}'".format(pic_url)
    sql = (
        "update share_pic set {}"
    )
    info = yield mysql_app_pool.update(sql.format(param))
    raise gen.Return(info)